" 定义快捷键的前缀，即<Leader>                                             
let mapleader=";"                                                          
"开启文件类型检测                                                          
filetype on                                                                
"根据检测到的类型加载不同的插件                                            
filetype plugin on                                                         
                                                                           
"让配置文件的更改立即生效                                                  
autocmd BufWritePost ${MYVIMRC} source ${MYVIMRC}                          
                                                                           
 " 开启实时搜索功能                                                        
set incsearch                                                              
 " 搜索时大小写不敏感                                                      
set ignorecase                                                             
" 高亮显示搜索结果                                                         
set hlsearch                                                               
 " 关闭兼容模式                                                            
set nocompatible                                                           
 " vim 自身命令行模式智能补全                                              
set wildmenu                                                               
                                                                           
" 总是显示状态栏                                                           
set laststatus=2                                                           
" 显示光标当前位置                                                         
set ruler                                                                  
" 开启行号显示                                                             
set number                                                                 
" 高亮显示当前行/列                                                        
set cursorline                                                             
"set cursorcolumn                                                          
                                                                           
" 开启语法高亮功能                                                         
syntax enable                                                              
" 允许用指定语法高亮配色方案替换默认方案                                   
syntax on                                                                  
set hlsearch
set t_Co=256
let g:molokai_original = 1
let g:rehash256 = 1
colorscheme molokai

"colorscheme darkblue

" 自适应不同语言的智能缩进                                                 
filetype indent on                                                         
" 将制表符扩展为空格                                                       
set expandtab                                                              
" 设置编辑时制表符占用空格数                                               
set tabstop=4                                                              
" 设置格式化时制表符占用空格数                                             
set shiftwidth=4                                                           
" 让 vim 把连续数量的空格视为一个制表符                                    
set softtabstop=4                                                          
                                                                           
" 基于缩进或语法进行代码折叠                                               
set foldmethod=indent                                                     
"set foldmethod=syntax                                                      
" 启动 vim 时关闭折叠代码                                                  
set nofoldenable                                                           
"za，打开或关闭当前折叠；zM，关闭所有折叠；zR，打开所有折叠                

" 记录上次离开文件的位置
if has("autocmd")
    au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") | exe "normal! g'\"" | endif 
endif
                                                                           
" vundle 环境设置                                                          
filetype off                                                               
set rtp+=~/.vim/bundle/Vundle.vim                                          
" vundle 管理的插件列表必须位于 vundle#begin() 和 vundle#end() 之间        
call vundle#begin()                                                        
                                                                           
"避免PluginClean把自己卸载了                                               
Plugin 'VundleVim/Vundle.vim'                                              
                                                                           
"实时显示符号定义插件 
Plugin 'vim-scripts/taglist.vim' 
Plugin 'majutsushi/tagbar'

"Plugin 'powerline/powerline'

"浏览文件结构插件                                                          
Plugin 'scrooloose/nerdtree'                                               
                                                                           
"同时编辑buffer插件                                                        
Plugin 'fholgado/minibufexpl.vim'                                          
                                                                           
"文件查找                                                                  
Plugin 'ctrlpvim/ctrlp.vim'                                                
                                                                           
"增强状态栏                                                                
Plugin 'vim-airline/vim-airline'                                           
Plugin 'vim-airline/vim-airline-themes'                                    

Plugin 'Valloric/YouCompleteMe'
                                                                           
                                                                           
" 插件列表结束                                                             
call vundle#end()                                                          
filetype plugin indent on                                                  
                                                                           
"安装插件，先找到其在 github.com 的地址，再将配置信息其加入 .vimrc   "中的call vundle#begin() 和 call vundle#end() 之间，最后进入 vim 执行
":PluginInstall 便可自动安装

"要卸载插件，先在 .vimrc 中注释或者删除对应插件配置信息，然后在 vim  "中执行:PluginClean 便可卸载对应插件
                                                           
"批量更新，只需执行:PluginUpdate                           


""""""taglist""""""""
"ctags位于PATH目录下,所以可以省略具体路径 
"let Tlist_Ctags_Cmd='ctags' 
"不同时显示多个文件的tag，只显示当前文件的 
"let Tlist_Show_One_File=1 
"设置taglist的宽度 
"let Tlist_WinWidth =40 
"如果taglist窗口是最后一个窗口，则退出vim 
"let Tlist_Exit_OnlyWindow=1 
"在右侧窗口中显示taglist窗口 
"let Tlist_Use_Right_Window=1 
"在左侧窗口中显示taglist窗口 
"let Tlist_Use_Left_Windo =1 
"启动vim后自动打开 
"let Tlist_Auto_Open=1 
"let Tlist_Use_Horiz_Window=1
"let Tlist_WinHeight = 20 
"map <F10> :TlistToggle<CR> 
"imap <F10> <ESC>:TlistToggle<CR>

""""""tagbar""""""
map <F10> :TagbarToggle<CR> 
imap <F10> <ESC>:TagbarToggle<CR>
let g:tagbar_ctags_bin = 'ctags'
let g:tagbar_width = 40
"tagbar一打开，光标即在tagbar页面内
let g:tagbar_autofocus = 1


""""""CTags"""""" 
map <F5> :!find . -name "*.h" -o -name "*.c" -o -name "*.cc" -o -name "*.cpp" > cscope.files <CR>:!cscope -Rbqk -i cscope.files<CR>:!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR> :TlistUpdate<CR><CR> :!cs kill cscope.out<CR><CR> :!cs add cscope.out<CR><CR>
imap <F5> <ESC>:!find . -name "*.h" -o -name "*.c" -o -name "*.cc" -o -name "*.cpp" > cscope.files <CR>:!cscope -Rbqk -i cscope.files<CR>:!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR> :TlistUpdate<CR><CR> :!cs kill cscope.out<CR><CR> :!cs add cscope.out<CR><CR>

set tags=./.tags;,.tags

"map gd :tj <C-R><C-W><CR>
"imap gd <ESC>:tj <C-R><C-W><CR>

"""""""""""cscope""""""""""""""
if has("cscope")
    "set csprg=/usr/bin/cscope
    " 优化查找cscope数据库,再查找tag文件
    set csto=0
    " 使用cstag代替默认的tag
    set cst
    " 当在增加一个cscope数据库时显示表示是否成功
    "set csverb
    if filereadable("cscope.out")
        cs add cscope.out 
    elseif $CSCOPE_DB != ""
        cs add $CSCOPE_DB
    endif
endif

" s 查找符号
" g 查找定义
" d 查找被这个函数调用的函数
" c 查找调用这个函数的函数
" t 查找这个字符串
" e 查找这个egrep匹配模式
" f 查找这个文件
" i 查找include这个文件的文件

map <F12> :cs find g <C-R>=expand("<cword>")<CR><CR>
imap <F12> <ESC> :cs find g <C-R>=expand("<cword>")<CR><CR>

nnoremap <leader>gl :cs find s <C-R>=expand("<cword>")<CR><CR>
nnoremap <leader>gd :cs find g <C-R>=expand("<cword>")<CR><CR>
inoremap <leader>gl <ESC>:cs find s <C-R>=expand("<cword>")<CR><CR>
inoremap <leader>gd <ESC>:cs find g <C-R>=expand("<cword>")<CR><CR>
" 水平分隔
nnoremap <leader>s :scs find s <C-R>=expand("<cword>")<CR><CR>
nnoremap <leader>g :scs find g <C-R>=expand("<cword>")<CR><CR>
nnoremap <leader>d :scs find d <C-R>=expand("<cword>")<CR><CR>
nnoremap <leader>c :scs find c <C-R>=expand("<cword>")<CR><CR>
nnoremap <leader>t :scs find t <C-R>=expand("<cword>")<CR><CR>
nnoremap <leader>e :scs find e <C-R>=expand("<cword>")<CR><CR>
nnoremap <leader>f :scs find f <C-R>=expand("<cword>")<CR><CR>
nnoremap <leader>i :scs find i <C-R>=expand("<cword>")<CR><CR>
" 垂直分隔
nnoremap <leader><leader>s :vert scs find s <C-R>=expand("<cword>")<CR><CR>
nnoremap <leader><leader>g :vert scs find g <C-R>=expand("<cword>")<CR><CR>
nnoremap <leader><leader>d :vert scs find d <C-R>=expand("<cword>")<CR><CR>
nnoremap <leader><leader>c :vert scs find c <C-R>=expand("<cword>")<CR><CR>
nnoremap <leader><leader>t :vert scs find t <C-R>=expand("<cword>")<CR><CR>
nnoremap <leader><leader>e :vert scs find e <C-R>=expand("<cword>")<CR><CR>
nnoremap <leader><leader>f :vert scs find f <C-R>=expand("<cword>")<CR><CR>
nnoremap <leader><leader>i :vert scs find i <C-R>=expand("<cword>")<CR><CR>

" 回跳
"map <C-T> <C-O>                 
"imap <C-T> <ESC><C-O>

""""""""""""NERDTree"""""""""""                                                    
"启动vim时自动打开NERDTree                                                  
"autocmd VimEnter * NERDTree                                                 
" 设置NERDTree子窗口宽度                                                    
let NERDTreeWinSize=30                                                      
" 设置NERDTree子窗口位置                                                    
let NERDTreeWinPos="left"                                                   
" 显示隐藏文件                                                              
let NERDTreeShowHidden=1                                                    
" NERDTree 子窗口中不显示冗余帮助信息                                       
let NERDTreeMinimalUI=1                                                     
" 删除文件时自动删除文件对应 buffer                                         
let NERDTreeAutoDeleteBuffer=1  
"没有文件时自动打开NERDTree
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists("s:std_in") | NERDTree | endif                             
" 只剩 NERDTree时自动关
autocmd bufenter * nested if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif 

nnoremap <F9> :NERDTreeToggle<CR>

""""""buffer 切换快捷键""""""                                               
map <leader>n :bn<CR>                                                       
map <leader>p :bp<CR>                                                       
                                                                            
                                                       
                                                                            
""""""""airline"""""""""""""
"let g:airline_powerline_fonts = 1
if !exists('g:airline_symbols')
    let g:airline_symbols = {}
endif

let g:airline_left_sep = ''
let g:airline_left_alt_sep = ''
let g:airline_right_sep = ''
let g:airline_right_alt_sep = ''

let g:airline#extensions#whitespace#enabled = 0
let g:airline#extensions#tagbar#enabled = 1

" 总是显示状态栏                                                            
set laststatus=2

"let g:Powerline_symbols = 'fancy'
""""""""airline-theme""""""""""
let g:airline_theme="bubblegum"

""""""""""""ctrlp""""""""""""""
let g:ctrlp_map = '<c-p>'
"let g:ctrlp_cmd = 'CtrlPMixed'
let g:ctrlp_cmd = 'CtrlP'
let g:ctrlp_working_path_mode = 'ca'
let g:ctrlp_by_filename = 1
let g:ctrlp_switch_buffer = 'Et'
set wildignore+=*/tmp/*,*.so,*.sw*,*.o,*.zip,*.a,*.zip,*.gz

"""""""""""ycm"""""""""""""""""
set completeopt=menu,longest                                                
let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/.ycm_extra_conf.py'    " 配置全局路径
let g:ycm_confirm_extra_conf = 0    " 每次直接加载该文件，不提示是否要加载
let g:ycm_min_num_of_chars_for_completion=2 " 从第1个键入字符就开始罗列匹配项
let g:ycm_seed_identifiers_with_syntax = 1  " 语法关键字补全
let g:ycm_key_list_select_completion = ['<Down>']  " 映射按键, 没有这个会拦截掉tab, 导致其他插件的tab不能用.
let g:ycm_key_list_previous_completion = ['<Up>']
let g:ycm_show_diagnostics_ui = 0
"让YouCompleteMe同时利用原来的ctags 
let g:ycm_collect_identifiers_from_tag_files = 1

"每次重新生成匹配项，禁止缓存匹配项
let g:ycm_cache_omnifunc=0
"不查询ultisnips提供的代码模板补全，如果需要，设置成1即可
let g:ycm_use_ultisnips_completer=0

" 跳到定义片
"map <F12> :YcmCompleter GoToDefinitionElseDeclaration<CR>
"imap <F12> <ESC> :YcmCompleter GoToDefinitionElseDeclaration <CR>
"map gd :YcmCompleter GoToDefinition <CR>
map gl :YcmCompleter GoToDeclaration <CR>


""""""""""""""""""""其它""""""""""""""""""""""""
map <C-F> :grep --include="*.cpp" -rn "<C-R><C-W>" ./
imap <C-F> <ESC> :grep --include="*.cpp" -rn "<C-R><C-W>" ./

if exists('$TMUX')
    set term=screen-256color
endif

inoremap <c-e> <End>
inoremap <c-a> <Home>
nnoremap <c-e> <End>
nnoremap <c-a> <Home>
inoremap <c-h> <Left>
inoremap <c-l> <Right>
inoremap <c-j> <Down>
inoremap <c-k> <Up>